package com.gqz.defect.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.poi.ExcelUtil;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.defect.entity.TEnDefectsSheetOperateEntity;
import com.gqz.defect.mapper.EnDefectsSheetOperateMapper;
import com.gqz.defect.mapstruct.EnDefectsSheetOperateMapstruct;
import com.gqz.defect.service.IEnDefectsSheetOperateService;
import com.gqz.defect.vo.EnDefectsSheetOperateVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class EnDefectsSheetOperateServiceImpl extends ServiceImpl<EnDefectsSheetOperateMapper, TEnDefectsSheetOperateEntity> implements IEnDefectsSheetOperateService {

    @Autowired
    private EnDefectsSheetOperateMapstruct mapstruct;

    @Override
    public List<EnDefectsSheetOperateVo> getById(String id) {
        MPJLambdaWrapper<TEnDefectsSheetOperateEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(TEnDefectsSheetOperateEntity::getId, id);
        queryWrapper.selectAll(TEnDefectsSheetOperateEntity.class);
        List<TEnDefectsSheetOperateEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnDefectsSheetOperateVo> page(RequestForm<EnDefectsSheetOperateVo> requestForm) {
        TEnDefectsSheetOperateEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnDefectsSheetOperateEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(TEnDefectsSheetOperateEntity.class);
        Page<TEnDefectsSheetOperateEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnDefectsSheetOperateEntity> iPage = baseMapper.selectJoinPage(page, TEnDefectsSheetOperateEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnDefectsSheetOperateVo> requestForm) {
        TEnDefectsSheetOperateEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnDefectsSheetOperateEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(TEnDefectsSheetOperateEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnDefectsSheetOperateVo> forms) {
        List<TEnDefectsSheetOperateEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnDefectsSheetOperateEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnDefectsSheetOperateVo> forms) {
        List<TEnDefectsSheetOperateEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            TEnDefectsSheetOperateEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @SneakyThrows
    @Override
    public void exportExcel(HttpServletResponse response, RequestForm<EnDefectsSheetOperateVo> requestForm) {
        //设置信息头，告诉浏览器内容为excel类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //文件名称
        String fileName = "null信息�?.xls";
        //sheet名称
        String sheetName = "null列表";
        fileName = new String(fileName.getBytes(), "ISO-8859-1");
        //设置下载名称
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        if (requestForm.getPageSize() < 1000L) {
            requestForm.setPageSize(1000L);
        }
        //获取到业务数�?
        Page<EnDefectsSheetOperateVo> page = this.page(requestForm);
        new ExcelUtil<>(EnDefectsSheetOperateVo.class).exportExcel(response, page.getRecords(), sheetName);

    }


}
